<div class="bjui-pageHeader">
    <div class="pull-right">
        <button class="btn-default" data-icon="refresh" onclick="$.CurrentNavtab.navtab('refresh')"></button>
    </div>
</div>
<div class="bjui-pageContent">
    <form action="{:U('save_model')}" data-toggle="validate" data-alertmsg="false">
        <input type="hidden" name="mid" value="{$va.id}"/>
        <div class="form-group">
            <label class="x100">模型名称:</label>
            <input type="text" name="name" data-rule="required" value="{$va.name}"/>
        </div>
        <div class="form-group">
            <label class="x100">规格项:</label>
            <input type="hidden" name="spec_name" class="types_call" placeholder="不要更改这个input, 只是拿来触发事件的" />
            <button type="button" class="btn-green" data-toggle="lookupbtn" data-title="选择规格" data-id="sel_types" data-width="800" data-height="500" data-url="{:U('Goods/spec', array('type'=>2))}">添加</button>
        </div>
        <div class="form-group spec">
            <table class="table table-bordered table-hover spec_table" data-selected-multi="false" <if condition="$va['spec'] eq null">style="display: none;"</if>>
                <thead>
                <tr>
                    <th width="200">规格名称</th>
                    <th>规格项</th>
                    <th>权重</th>
                    <th width="70">操作</th>
                </tr>
                </thead>
                <tbody>
                <if condition="$va['spec'] neq null">
                    <php>$countspec = count($va['spec']);</php>
	                <volist name="va['spec']" id="vaa" key="i">
	                   <tr data-id="{$vaa.id}"><td>{$vaa.name}</td><td>{$vaa.value}</td><td><input type="hidden" name="spec_ids[]" value="{$vaa.id}"><input type="text" name="spec_sort[]" class="form-control" value="{$countspec-$i}"></td><td><button type="button" class="btn btn-red">删</button></td></tr>
	                </volist>
                </if>
                </tbody>
            </table>
        </div>
        <div class="form-group">
            <label class="x100">属性参数:</label>
            <input type="hidden" name="attr_name" class="attr_call" placeholder="不要更改这个input, 只是拿来触发事件的" />
            <button type="button" class="btn-green" data-toggle="lookupbtn" data-title="选择属性" data-id="sel_attr" data-width="800" data-height="500" data-url="{:U('Goods/attr', array('type'=>2))}">添加</button>
        </div>
        <div class="form-group">
            <table class="table table-bordered table-hover attr_table" data-selected-multi="false" <if condition="$va['attr'] eq null">style="display: none;"</if> >
                <thead>
                <tr>
                    <th>属性名称</th>
                    <th>输入方式</th>
                    <th>可选值</th>
                    <th>权重</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <if condition="$va['attr'] neq null">
                <volist name="va['attr']" id="vao" key="k">
                   <php>
                        $countattr = count($va['attr']);
				        switch($vao['type']){
				           case '1':
				             $typename = "单选";
				           break;
				           case '2':
				             $typename = "多选";
				           break;
				           case '3':
				             $typename = "下拉";
				           break;
				           case '4':
				             $typename = "文本输入";
				           break;
				           case '5':
				             $typename = "多行文本输入";
				           break;
				        };
			        </php>
                   <tr data-id="{$vao.id}"><td>{$vao.name}</td><td>{$typename}</td><td>{$vao.value}</td><td><input type="hidden" name="attr_ids[]" value="{$vao.id}"><input type="text" name="attr_sort[]" value="{$countattr-$k}"></td><td><button type="button" class="btn btn-red">删</button></td></tr>
                </volist>
                </if>
                </tbody>
            </table>
        </div>

    </form>
</div>
<script>
    $(function () {
        function compose(tab, data,funback) {
            var result = data.args;
            var arr = [];
            if( (result.hasOwnProperty('id') && (result.id + "").indexOf(',') < 1) ){
                arr.push(result);
            }else{
                for (var item in result){
                    var text = result[item].split(',');
                    if( arr.length < 1 ){
                        for( var index = 0; index < text.length; index++ ){
                            var o = {};
                            o[item] = text[index];
                            arr.push(o);
                        }
                    }else{
                        for(var index = 0 ; index < arr.length ; index++){
                            arr[index][item] = text[index];
                        }
                    }
                }
            }
            var goodsTableBody = $('tbody', tab),
                    otr = goodsTableBody.find('tr'),
                    proidArr = [];
            if( otr.length < 1 ) {
                tab.show();
            }
            otr.each(function (i, item) {
                proidArr.push(parseInt($(this).attr('data-id')));
            });
            var htmlString = '';
            for (var index = 0 ; index < arr.length; index++){
                if( proidArr.length > 0 && $.inArray(parseInt(arr[index].id), proidArr) != -1 ){
                    continue;
                }else{
//                     在tr上放着ID,去重
                    htmlString += '<tr data-id="'+arr[index].id+'">';
                    htmlString += funback.call(this, arr[index]);
                    htmlString += '</tr>';
                }
            }
            goodsTableBody.append(htmlString);
        }
//        规格
        $('.types_call').on('afterchange.bjui.lookup', function (e, result) {
            compose($('.spec_table'), result, function (ret) {
                var str = '<td>'+ret.spec_name+'</td><td>'+ ret.spec_item.split('_').join(',')+'</td>';
                    str += '<td><input type="hidden" name="spec_ids[]" value="'+ret.id+'" /><input type="text" name="spec_sort[]" class="form-control" value="0" /></td>';
                    str += '<td><button type="button" class="btn btn-red">删</button></td>';
                return str;
            });
        });
//        属性
        $('.attr_call').on('afterchange.bjui.lookup', function (e, result) {
            compose($('.attr_table'), result, function(ret){
                var str = '<td>'+ret.attr_name+'</td><td>'+ret.attr_input+'</td>';
                    str += '<td>'+(function(item){
                                if( item ){
                                    if( item.indexOf('_') > 0 ){
                                        return item.split('_').join(',');
                                    }
                                }
                                return item || "";
                            })(ret.attr_item)+'</td>';
                    str += '<td><input type="hidden" name="attr_ids[]" value="'+ret.id+'"/><input type="text" name="attr_sort[]" value="0" /></td>';
                    str += '<td><button type="button" class="btn btn-red">删</button></td>';
                return str;
            });
        });
        $('.attr_table,.spec_table').on('click', 'button.btn-red', function () {
            $(this).parents('tr').remove();
        });

    });
</script>
<include file="Common/footer" ftype="save" />